<template>
	<view>
		<view class="logo">
			<img src="https://statics.xmcsrv.net/weixin/logo-xfgj.png" alt="">
		</view>
		<view v-if="loading" class="">
			<view v-if="isShareSingleDevice" class="shareSingleTitle">
				<template v-if="loseEfficacy">
					<view>
						发现用户[<text class="mainColor">{{decodeInfo.nickName}}</text>]分享的设备[<text class="mainColor">{{decodeInfo.deviceName}}</text>]
					</view>
					<text>是否添加？</text>
				</template>
				<template v-else>
					<view>{{shareHint}}</view>
				</template>
			</view>
			<view v-else class="shareTitle">
				{{shareHint}}
			</view>
			<!-- <view v-if="!loseEfficacy" class="shareHint">
				原因1：此邀请的有效时间是5分钟，若超过时间后，此链接失效。
			</view> -->
			<view v-if="!loseEfficacy" class="shareHint">
				原因：此链接仅第一个点击的用户可用。
			</view>
			<button v-if="loseEfficacy" class="block agree radius-btn" @tap="nextagree()" type="operation">同意</button>
			<button class="block back radius-btn" @tap="back()" type="operation">返回</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				shareHint: "邀请你加入家庭",
				loseEfficacy: false,
				loading: false,
				shareMessage: null,
				isShareSingleDevice:false,
				decodeInfo:{}
			}
		},
		onLoad(opt) {
			console.log(decodeURIComponent(opt.shareMessage))
			this.shareMessage = decodeURIComponent(opt.shareMessage)
			console.log('加入分享',opt);
			// this.shareMessage = '请长按复制邀请码，打开app加入用户组：#￥7&@6f$820@2b￥#'
			// this.shareMessage = '请长按复制分享码，打开app接收设备：##&6513d^a@^0d##'
			this.$logs({info:'加入分享获取分享码',key:this.shareMessage})
		},
		onShow() {
			const accessToken = uni.getStorageSync('accessToken') || '';
			const userInfo = uni.getStorageSync('userInfo') || {};
			console.log(accessToken, userInfo, '用户信息')
			if (accessToken !== '' && (typeof userInfo == 'object') && Object.keys(userInfo).length > 0) {
				let shareMessage = this.shareMessage
				if (shareMessage.includes('请长按复制邀请码，打开app加入用户组')) {
					this.loseEfficacy = true
					if (uni.getStorageSync('inviteCode') != shareMessage.split(':')[1]) {
						this.decodeKey(shareMessage)
						uni.setStorageSync('inviteCode', shareMessage.split(':')[1])
					}
				}
				if (shareMessage.includes('请长按复制分享码，打开app接收设备')) {
					this.loseEfficacy = true
					this.isShareSingleDevice = true
					this.decodeShareCode(shareMessage)
				}
				this.getUserGroupListByPage()
			} else {
				uni.navigateTo({
					url: '/jvss/pages/login/login?type=share'
				});
			}

		},
		methods: {
			back() {
				uni.reLaunch({
					url: '/jvss/pages/device/home'
				});
			},
			// 解析邀请口令
			decodeKey(key) {
				uni.showLoading();
				const data = {
					key: key
				}
				// 解析邀请码
				getApp().globalData.api.decodeKey(data, result => {
					uni.hideLoading();
					console.log('解析邀请码：', result);
					this.loading = true
					if (result.data.code === 2000) {
						this.shareHint = '是否加入家庭组' + result.data.data.userGroupName
						this.loseEfficacy = true
					} else {
						this.loseEfficacy = false
						this.shareHint = result.data.msg
					}
					this.$logs({info:'解析邀请码',key:key,result:result})
				})
			},
			// 解析分享单个设备邀请口令
			decodeShareCode(key) {
				uni.showLoading();
				const data = {
					key: key
				}
				// 解析邀请码
				getApp().globalData.api.decodeShareCode(data, result => {
					uni.hideLoading();
					console.log('解析邀请码1：', result);
					this.loading = true
					if (result.data.code === 2000) {
						this.decodeInfo = result.data.data
						this.loseEfficacy = true
					} else {
						this.loseEfficacy = false
						this.shareHint = result.data.msg
					}
					this.$logs({info:'解析单个设备分享邀请码',key:key,result:result})
				})
			},
			//单个设备分享添加
			acceptShareByCode(){
				uni.showLoading();
				let params = {
					key:this.shareMessage
				}
				getApp().globalData.api.acceptShareByCode(params, result => {
					uni.hideLoading();
					this.getUserGroupListByPage(1)
				})
				this.$logs({info:'加入分享的单个设备',key:this.shareMessage,userInfo:uni.getStorageSync('userInfo') || ''})
			},
			// 加入家庭
			nextagree(key) {
				if(this.isShareSingleDevice){
					this.acceptShareByCode()
					return
				}
				uni.showLoading();
				const params = {
					userName: uni.getStorageSync('userInfo').nickName || uni.getStorageSync('userInfo').nickname || uni.getStorageSync('userInfo').username,
					key: this.shareMessage,
				}
				// 添加用户到家庭组
				getApp().globalData.api.addUser(params, result => {
					uni.hideLoading();
					uni.setStorageSync('currentGroupId',result)
					this.getUserGroupListByPage(1)
				})
				this.$logs({info:'加入分享家庭',key:this.shareMessage,userInfo:uni.getStorageSync('userInfo') || ''})
			},
			// 查询用户组列表
			getUserGroupListByPage(type) {
				let data = {
					page: 1,
					limit: 999
				}
				getApp().globalData.api.getUserGroupListByPage(data, (result) => {
					uni.setStorageSync('groupList', result.data);
					if(type == 1){
						uni.reLaunch({
							url: '/jvss/pages/device/home'
						});
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.logo {
		text-align: center;
		margin-top: 160rpx;

		>img {
			width: 140rpx;
			height: 140rpx;
		}

	}

	.shareTitle {
		text-align: center;
		font-size: 30rpx;
		font-weight: 600;
		margin: 30rpx 0;
	}

	.shareHint {
		color: #666;
		text-align: center;
		padding: 40rpx;
	}

	.block {
		width: 50% !important;
		border-radius: 48rpx;
		margin-top: 20rpx;
		border: none !important;
	}

	.agree {
		color: #fff !important;
		background-color: #FF0025 !important;
	}

	.back {
		color: #333 !important;
		background-color: #eee !important;
	}

	button::after {
		border: none !important;
	}
	.shareSingleTitle{
		color: #666;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		padding: 20rpx 50rpx;
		text-align: center;
	}
	.mainColor{
		color: #FF0025;
	}
</style>
